// vue 观察者模式
// 被观察者-依赖   -> 宝宝
class Watcher {
  constructor(state) {
    this.state = state
    // 存储观察者
    this.dep = []
  }
  // 绑定观察者
  attach(...obs) {
    obs.forEach(o => this.dep.push(o))
  }
  // 通知观察者执行更新方法
  notify(newState) {
    let oldState = this.state
    this.state = newState
    this.dep.forEach(observer => observer.update(newState, oldState))
  }
}

// 观察者-响应式数据  -> 爸爸、妈妈、爷爷、奶奶
class Observer {
  constructor(name) {
    this.name = name
  }
  // 执行更新方法
  update(newState, oldState) {
    console.log(`${this.name}：刚才宝宝状态是${oldState}，现在是${newState}`)
  }
}

let baby = new Watcher('开心')
let ob1 = new Observer('爸爸')
let ob2 = new Observer('妈妈')
baby.attach(ob1, ob2)
baby.notify('不开心')